﻿@using LNFramework.Common.Extends
@using LNFramework.Mvc.Extends
@inherits BaseWebViewPage
@{
    ViewBag.Title = "登录";
}

@*加入這一段主要是為了有jquery智能提示*@
@if (false)
{
    <script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="../../Scripts/extjs4.2.1/ext-all-debug.js" type="text/javascript"></script>
}
@section FooterScript
{
    <script type="text/javascript">
        Ext.onReady(function () {
            var form = Ext.create('Ext.form.Panel', {
                defaults: {
                    anchor: '100%',
                    labelSeparator: "：",
                    labelWidth: 80,
                    labelAlign: "left"
                },
                bodyStyle: 'padding:10px;border:0',
                layout: {
                    type: 'anchor'
                },
                items: [{
                    xtype: 'textfield',
                    fieldLabel: '用户名',
                    name: 'userName'
                }, {
                    xtype: 'textfield',
                    fieldLabel: '密码',
                    name: 'pwd',
                    inputType: 'password'
                }],
                buttons: [{
                    text: '登录',
                    handler: function () {
                        form.onLogin();
                    }
                }],
                onLogin: function () {
                    if (form.getForm().isValid()) {
                        var username = $("input[name='userName']").val();
                        var password = $("input[name='pwd']").val();

                        //TODO: 登录验证
                        Ext.Ajax.request({
                            url: '@Url.Action("CheckLogin","Home")',
                            method: 'GET',
                            params: {
                                username: username,
                                password: password
                            },
                            success: function (response, opts) {
                                var obj = Ext.decode(response.responseText);
                                if (obj.Success == true) {
                                    //top.location.href = '/Home/Index';
                                    top.location.href = '@Url.Action("Index", "Home")';
                                }
                                else {
                                    var obj = Ext.decode(response.responseText);
                                    Ext.MessageBox.alert('提示', obj.Msg);

                                    // 清空输入
                                    $("input[name='userName']").val("");
                                    $("input[name='pwd']").val("");

                                    // 让焦点定位在用户名上
                                    $("input[name='userName']").focus(); // 为什么没有效果？
                                }
                            },
                            failure: function (response, opts) {
                                var obj = Ext.decode(response.responseText);
                                Ext.MessageBox.alert('提示', obj.Msg);
                            }
                        });

                    }
                },
                listeners: {
                    render: function (panel) {
                        panel.el.on('keypress', function (e) {
                            if (e.getKey() == e.ENTER) {
                                form.onLogin();
                            }
                        });
                    }
                }
            })

            Ext.create('Ext.window.Window', {
                title: '请登录',
                layout: 'fit',
                width: 300,
                closable: false,
                draggable: false,
                items: form
            }).show();

        });
    </script>
}
